﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using Legazpi.Payroll.DataAccess.Base;
using Legazpi.Payroll.DataAccess.Repositories;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using System.Linq.Expressions;

namespace Legazpi.Payroll.BusinessLogic.Services
{
    public class PayrollSummaryService : IPayrollSummaryService
    {
        #region Fields

        private DbContext _dbContext;
        private IPayrollSummaryRepository _payrollSummaryRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public PayrollSummaryService()
            : this(new DatabaseFactory())
        {

        }

        public PayrollSummaryService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _payrollSummaryRepository = new PayrollSummaryRepository(_dbContext);
            }
        }

        #endregion


        public PayrollSummary GetById(int id)
        {
            return _payrollSummaryRepository.GetById(id);
        }

        public IEnumerable<PayrollSummary> GetAll()
        {
            return _payrollSummaryRepository.GetAll();
        }

        public IEnumerable<PayrollSummary> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(PayrollSummary payrollSummary)
        {
            _payrollSummaryRepository.Add(payrollSummary);
        }

        public void Update(PayrollSummary payrollSummary)
        {
            _payrollSummaryRepository.Update(payrollSummary);
        }

        public void Delete(int id)
        {
            var payrollSummary = GetById(id);
            if (payrollSummary != null)
                _payrollSummaryRepository.Delete(payrollSummary);
        }

        public bool Save()
        {
            string message;
            _payrollSummaryRepository.Save(out message);
            return message == "Saved";
        }

        public PayrollSummary GetEmployeePayrollSummary(int employeeId, int payrollId)
        {
            return _payrollSummaryRepository.Get(x => x.EmployeeId == employeeId && x.PayRollId == payrollId);
        }

        public IEnumerable<PayrollSummary> GetPayrollSummary(int payrollId)
        {
            return _payrollSummaryRepository.GetMany(x => x.PayRollId == payrollId);
        }

        public void ResetHours(int employeeId, int payrollId)
        {
            var payrollSummary = GetEmployeePayrollSummary(employeeId, payrollId);
            if (payrollSummary != null)
            {
                payrollSummary.RegularHours = 0;
                payrollSummary.LegalHours = 0;
                payrollSummary.SpecialHours = 0;
                payrollSummary.OverTime = 0;
                payrollSummary.OverTimeSpecial = 0;
                payrollSummary.OverTimeLegal = 0;
                payrollSummary.NightDiff = 0;
                payrollSummary.NightDiffSpecial = 0;
                payrollSummary.NightDiffLegal = 0;
                payrollSummary.CoverUpHours = 0;

                Update(payrollSummary);
            }            
        }


        public System.Linq.IQueryable<PayrollSummary> GetAllByQuery()
        {
            return _payrollSummaryRepository.GetQuery();
        }
    }
}
